<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['type']=array(1=>'售后问卷',2=>'售后主管问卷');
if($_RQ['op']=='list'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['type']){
		$params['type']=$type;
		$return['params']['type']=$type;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_yxw_diaoca_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);			
			$item['url']=custom_url('jy_yxw/diaoca//id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$return['group']=$group?$group:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'问卷列表'),
			array('op'=>'edit','title'=>'添加问卷'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：选择题库类型，自动绑定相同题库的题目！','list'=>array(
			array('name'=>'title','title'=>'问卷标题','type'=>'string'),
			array('name'=>'type','title'=>'题库类型','type'=>'radio','options'=>'type'),
			array('name'=>'readnum','title'=>'阅卷量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'export','action'=>'/shouhou.diaoca_list/export/','title'=>'导出题目'),
			array('type'=>'export','action'=>'/shouhou.diaoca_list/export1/','title'=>'导出问卷记录'),
			array('type'=>'href','action'=>'/shouhou.diaoca_log/from/','title'=>'查看售后参与记录'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'问卷标题','type'=>'string'),
			array('name'=>'type','width'=>10,'title'=>'题库类型','type'=>'radio','options'=>'type'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_yxw_diaoca_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_yxw_diaoca_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_yxw_diaoca_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_diaoca_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('status'=>1);
	}
	$item['hdp']=json_decode($item['hdp'],true);
	$item['typenum']=json_decode($item['typenum'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问卷列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'名称','type'=>'string','must'=>true),
					array('name'=>'type','title'=>'题库类型','type'=>'radio','options'=>'type','help'=>'选择题库类型，自动绑定相同题库的题目！'),
					array('name'=>'image','title'=>'logo','type'=>'image'),
					array('name'=>'desc','title'=>'描述','type'=>'text'),
					array('name'=>'hdp','title'=>'轮播图','type'=>'images'),
					array('name'=>'score','title'=>'题目总分','type'=>'unitstring','unit'=>'分'),
					array('name'=>'jigescore','title'=>'及格线分','type'=>'unitstring','unit'=>'分'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','image','hdp','times','score','type','jigescore','star','desc','status');
	$data=array_fields($fields,$postdata);
	$data['hdp']=ijson_encode($data['hdp']);
	if(empty($data['title'])){
		exi('请填写问卷标题！','error');
	}
	if(empty($data['type'])){
		exi('请绑定类型！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_yxw_diaoca_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑问卷成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$_SESSION['storeid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_yxw_diaoca_list',$data);
		$id=pdo_insertid();
		exi('添加问卷成功！','','list');
	}
}
if($_RQ['op']=='export'){
	if(empty($_RQ['id'])){
		exi('请选择问卷！','error');
	}
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'datiid'=>$_RQ['id']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$this->model('diaoca')->down_csv($params);
}
if($_RQ['op']=='export1'){
	if(empty($_RQ['id'])){
		exi('请选择问卷项目！','error');
	}
	$filename='candel/'.random(15);
	@mkdir(ROOT_AT.'candel/');
	@mkdir(ROOT_AT.$filename);
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$params['datiid']=intval($_RQ['id']);
	$diaoca=pdo_get('jy_yxw_diaoca_list',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$diaoca['classtitle']=pdo_getcolumn('jy_yxw_class',array('id'=>$diaoca['classid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$workers=pdo_getall('jy_yxw_worker',array('juese'=>$diaoca['type']==1?10:11,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($workers as $item){
		$params['workerid']=intval($item['id']);
		$list=pdo_getall('jy_yxw_diaoca_log',$params);
		foreach($list as &$rv){
			$rv['title']=pdo_getcolumn('jy_yxw_yonghu',array('id'=>$rv['yonghuid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$rv['workertitle']=$item['title'];
		}
		paixu($list,'allfen','desc');
		foreach($list as $k=>&$nv){
			$nv['key']=$nv['rank']=$k+1;
		}
		$header=array(
			'key'=>'编号',
			'title'=>'参与售后',
			'classtitle'=>'售后群',
			'workertitle'=>'授课售后',
			'dayid'=>'填卷时间',
		);
		if($diaoca['type']==1){
			$title='[售后]';
			$title .=$item['title'];
		}
		cfc('tools')->export2excel($header,$list,$title.'-问卷记录',false,$filename);
	}
	$baoname=$diaoca['title'].'-问卷'.random(3);
	cfc('tools')->down_zip(ROOT_AT.$filename,$baoname,true);
}
